package ar.fiuba.fallar2.pac_man.fruta.persist;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import ar.fiuba.fallar2.pac_man.AbstractDataMapper;
import ar.fiuba.fallar2.pac_man.DBManager;
import ar.fiuba.fallar2.pac_man.IBusinessObject;
import ar.fiuba.fallar2.pac_man.fruta.model.Fruta;

public class FrutaDataMapper extends AbstractDataMapper{

	@Override
	public List findAll() 
	{
		try {
			List<IBusinessObject> lista = new ArrayList<IBusinessObject>();
			String sqlQuery = "SELECT * FROM "+getTable();
			ResultSet r = DBManager.getInstance().find(sqlQuery).getResultSet();
			while (r.next()) 
				lista.add(createBO(r));
			
			r.getStatement().close();
			return lista;
		 } catch (SQLException e) {
			 e.printStackTrace();
			throw new RuntimeException(e);
		}
	
	}

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save(IBusinessObject obj) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public IBusinessObject load(int id) {
		// TODO Auto-generated method stub
		return null;
	}
	

@Override
protected IBusinessObject createBO(ResultSet r) throws  SQLException {
	
	 Fruta fruta= new Fruta(r.getInt("FT_id"),
			 				r.getInt("FT_Valor"),
			 			    r.getInt("FT_Color"));
			 				  		  	
	return  fruta;
}

@Override
protected String getIdColumn() {
	// TODO Auto-generated method stub
	return "FT_id";
}

@Override
protected String getTable() {
	// TODO Auto-generated method stub
	return "Fruta";
}

@Override
protected String mapBOInsert(IBusinessObject r) throws SQLException {
	Fruta fruta = (Fruta) r;
	StringBuffer sb = new StringBuffer();
	sb.append("INSERT INTO " + getTable());
	sb.append("(FT_Color ,FT_Valor) VALUES (");

	sb.append(fruta.getColor()+", '");
	sb.append(fruta.getValor()+"') ");
	return sb.toString();
}

@Override
protected String mapBOUpdate(IBusinessObject r) throws SQLException {

	Fruta fruta   = (Fruta) r;
	StringBuffer sb = new StringBuffer();
	sb.append("UPDATE " + getTable());
	sb.append("  FT_Color='"+fruta.getColor());
	sb.append(", FT_Valor='"+fruta.getValor());
	sb.append("' WHERE FT_Id="+fruta.getId()+")");
	return sb.toString();
}
	

}
